class CommentsController < ApplicationController
  session :disabled => false
  cache_sweeper :site_sweeper, :only => [:new]
  
  def new
    return head(404) unless 
      request.post? &&
      !params[:body].blank? &&
      !params[:name].blank? &&
      (!(params[:name].to_s.downcase =~ /eusden/) || admin?) &&
      params[:commit].to_s == "Submit"
      
    # 403 for blocked IPs
    ip_address = request.env['HTTP_X_HULU_CLIENT_IP'] || request.remote_ip
    return head(403) if ::BLOCKED_IPS["list"].include?(ip_address)
    
    if cookies[:comment_test] != ::SECRET["comment_key"]
      if params[:spam_test] != "moo"
        return head(401)
      else
        set_cookie(:comment_test, ::SECRET["comment_key"], 1.year)
      end
    end
    
    # handle parameters
    if !params[:commentable_type].blank?
      begin
        target = Object.const_get(params[:commentable_type].to_s).find(params[:commentable_id].to_i)
      rescue
        return head(404)
      end
    else
      target = nil
    end

    comment = Comment.new({
      :name       => params[:name].to_s,
      :email      => params[:email].to_s,
      :body       => params[:body].to_s,
      :guid       => params[:guid].to_s,
      :ip_address => ip_address
    })
    comment.commentable = target
      
    error = ""
    # No yelling
    num_caps = 0
    comment.body.each_char {|c|
      num_caps = num_caps + 1 if c >= "A" && c <= "Z" 
    }
    if num_caps > (comment.body.size * 0.7)
      error = "Please do not yell."
    end
    
    # Don't accept links cuz of spam
    if comment.body =~ /http[s]{0,1}:\/\//
      return head(403)
    end
    
    if error.blank? && comment.save
      set_cookie(:name, comment.name, 1.year)
      set_cookie(:email, comment.email, 1.year) unless comment.email.blank?
      
      render :update do |page|
        case params[:commented_from].to_s
          when "bar"
            page.insert_html(
              :bottom,
              "bar-comments-list",
              :partial => "comments/bar_item",
              :locals => { :comment => comment, :admin => false }
            )
            page << "$('bar-comment-#{comment.id}').hide();"
            page << "$('bar-comment-#{comment.id}').appear();"
          when "default"
            page.insert_html(
              :bottom,
              "comments-list-#{target.class.to_s.downcase}-#{target.id}",
              :partial => "comments/item",
              :locals => { :comment => comment }
            )
            page << "$('comment-#{comment.id}').hide();"
            page << "$('comment-#{comment.id}').appear();"
          when "lightbox"
            page.insert_html(
              :bottom,
              "lightbox-comments-list",
              :partial => "comments/lightbox_item",
              :locals => { :comment => comment }
            )
            page << "$('lightbox-comment-#{comment.id}').hide();"
            page << "$('lightbox-comment-#{comment.id}').appear();"
        end
      end
    else
      if error.blank?
        errors = ""
        comment.errors.each {|k,v| errors = "#{v} "}
      else
        errors = error
      end
      render :update do |page|
        page << "Submit.current_form.update(#{errors});"
      end
    end
  end
  
  def list 
    return head(404) if params[:commentable_type].blank? || params[:commentable_id].blank?
    
    commentable = Object.const_get(params[:commentable_type].to_s).find(params[:commentable_id].to_i)
    return head(404) unless commentable

    render :update do |page|
      page.insert_html(
        :bottom,
        "lightbox-comments-list",
        :partial => "comments/lightbox_list",
        :locals => { :comments => commentable.comments }
      )
    end
  end
end
